using UnityEngine;
using System.Collections;

public class InterfaceRenderer : MonoBehaviour {

    static Vector3 planeClickPos;
    void OnGUI() { 

        //Find object clicked
        if (Input.GetMouseButtonDown(0)) {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit)){
                if (hit.collider != null) {
                    planeClickPos = Input.mousePosition;
                    Debug.Log(hit.transform.gameObject.name);
                    if (Constants.gameState == Constants.GameState.Planning) {
                        if (hit.transform.gameObject.tag == "Airport") {
                            Interface.activeAirport = hit.transform.gameObject.GetComponent<Airport>();
                            Interface.currentMenuState = Interface.MenuStates.ATC;
                        } else if (hit.transform.gameObject.tag == "Aircraft") {

                            Interface.activeAircraft = hit.transform.gameObject.GetComponent<Aircraft>();
                        }
                    }else if (Constants.gameState == Constants.GameState.Playing){
                        if (hit.transform.gameObject.tag == "Aircraft") { 
                            Interface.activeAircraft = hit.transform.gameObject.GetComponent<Aircraft>();
                            switch (Interface.activeAircraft.flightPhase) { 
                                case Aircraft.FlightPhase.departing:
                                    Interface.activeAircraft.TakeOffAeroplane(Interface.activeAircraft.departureAirport);
                                    break;
                                case Aircraft.FlightPhase.holding:
                                    Interface.activeAircraft.RecievePermissionToLand();
                                    break;
                            }
                        }
                    }
                }
            }
        }

        Rect interfaceRect = new Rect(planeClickPos.x, Screen.height - planeClickPos.y, 200, 100);
        GUILayout.BeginArea(interfaceRect);
        if (Constants.gameState == Constants.GameState.Planning && Interface.activeAircraft != null) {
            GUILayout.Box("Plane bound for " + Interface.activeAircraft.destinationAirport.airportName.ToString());
        }
        GUILayout.EndArea();


    }
}
